package util

import (
	"bytes"
	"strings"
)

// KeyValuePair 键值对
type KeyValuePair struct {
	Key string `json:"key"`; 
	Value string `json:"value"`;
}

// FromJSON string -> KeyValuePair
func FromJSON(s string) (v KeyValuePair, err error) {
	if err = json.NewDecoder(strings.NewReader(s)).Decode(&v); err != nil {
		return
	}
	return
}

// SliceFromJSON string -> []KeyValuePair
func SliceFromJSON(s string) (v []KeyValuePair, err error) {
	if err = json.NewDecoder(strings.NewReader(s)).Decode(&v); err != nil {
		return
	}
	return
}

// SliceToJSON []KeyValuePair -> bytes[]
func SliceToJSON(v []KeyValuePair) (r []byte, err error) {
	if err = json.NewEncoder(bytes.NewBuffer(r)).Encode(&v); err != nil {
		return
	}
	return
}